<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\common\lib\Show;
use app\common\lib\StatusLib;
use think\facade\Request;
use app\admin\business\Module as BusiModule;
use think\facade\View;

class Module extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $condition = ['pid' => Request::param('pid', 0, 'intval')];

        try {
            $busiModule = new BusiModule();
            $result     = $busiModule->getLists($condition);
        } catch (\Exception $e) {
            $result     = [];
        }

        return View::fetch('', [
            'module' => $result,
            'deleteStatus' => config('status.mysql.table_normal')
        ]);
    }

    /**
     * 根据当前栏目获取所有下级栏目
     *
     * @return array
     */
    public function getChildCate()
    {
        $condition = [
            'pid' => Request::param('pid', 0, 'intval')
        ];

        try {
            $busiModule = new BusiModule();
            $result     = $busiModule->getLists($condition);
        } catch (\Exception $e) {
            $result     = [];
        }

        return Show::success($result);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        $pid = Request::param('id', 0, 'intval');

        $busiModule = new BusiModule();
        $result     = $busiModule->getNormalModule('id,name');
        return View::fetch('', [
            'module' => json_encode($result),
            'pid'    => $pid
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        // 获取提交数据
        $data = input('param.');

        if ($data['type'] == 1) {
            if (isset($data['module'])) unset($data['module']);
            if (isset($data['action'])) unset($data['action']);
        } else {
            $data['condition'] = "admin/{$data['module']}/{$data['action']}";
        }

        $validate = new \app\admin\validate\Module();
        if (!$validate->check($data)) {
            return Show::error($validate->getError());
        }

        try {
            $busiModule = new BusiModule();
            $insert     = $busiModule->addModule($data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $insert ? Show::success([], '栏目添加成功') : Show::error('栏目添加失败');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(int $id)
    {
        $busiModule = new BusiModule();
        try {
            $data = $busiModule->getById($id);
        } catch (\Exception $e) {
            $data = [];
        }

        try {
            $result = $busiModule->getNormalModule('id,name');
        } catch (\Exception $e) {
            $result = [];
        }
        return View::fetch('', [
            'module' => json_encode($result),
            'data'   => $data
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(int $id)
    {
        // 获取提交数据
        $data = input('param.');

        if ($data['type'] == 1) {
            if (isset($data['module'])) unset($data['module']);
            if (isset($data['action'])) unset($data['action']);
        } else {
            $data['condition'] = "admin/{$data['module']}/{$data['action']}";
        }

        $validate = new \app\admin\validate\Module();
        if (!$validate->check($data)) {
            return Show::error($validate->getError());
        }

        try {
            $busiModule = new BusiModule();
            $insert     = $busiModule->edit($id, $data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $insert ? Show::success([], '栏目添加成功') : Show::error('栏目添加失败');
    }

    /**
     * 修改分类状态
     *
     * @param Request $request
     * @return \think\response\Json
     * @throws Exception
     */
    public function status()
    {
        // 获取当前提交数据
        $data = [
            'status' => Request::param('status', 0, 'intval'),
            'id' => Request::param('id', 0, 'intval')
        ];

        // 判断 id 和 状态是否正确
        if (!$data['id'] || !in_array($data['status'], StatusLib::getTableStatus())) return Show::error('参数错误');

        try {
            $busiCate = new BusiModule();
            $res = $busiCate->updateStatusById($data['id'], $data['status']);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $res ? Show::success() : Show::error('状态修改失败');
    }

    /**
     * 分类排序修改
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function listorder()
    {
        // 获取提交数据
        $data = [
            'id'        => Request::param('id', 0, 'intval'),
            'listorder' => Request::param('sort', 0, 'intval')
        ];

        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('listorder')->check($data)) {
            return Show::error($validate->getError());
        }

        try{
            $busiCate = new BusiModule();
            $res      = $busiCate->updateListorderById($data['id'], $data['listorder']);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $res ? Show::success([], '修改成功') : Show::error('修改失败');
    }

    public function updateType(int $id)
    {
        $data = ['type' => Request::param('type', 0, 'intval')];
        if (!$data['type']) return Show::error('参数错误');

        try {
            $module = new BusiModule();
            $res    = $module->edit($id, $data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }
        return $res ? Show::success([], '修改成功') : Show::error('修改失败');
    }

    public function updateIsShow(int $id)
    {
        $data = ['is_show' => Request::param('is_show', 0, 'intval')];

        try {
            $module = new BusiModule();
            $res    = $module->edit($id, $data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }
        return $res ? Show::success([], '修改成功') : Show::error('修改失败');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
